from common import data_check


def info_query(args):
    parameter_list = list(args.strip().split())
    print(parameter_list)
    if not len(parameter_list):
        return ""
    if len(parameter_list) % 4:
        return "只能输入 ip或域名 端口 库名 从账号 4个一组的数据！"

    group_num = int(len(parameter_list) / 4)
    print(group_num)
    result = ''

    for i in range(group_num):
        db_name = str(parameter_list[i * 4]).replace('[', '').replace(']', '').replace(' ', '')
        ip_addr = str(parameter_list[i * 4 + 1]).replace('[', '').replace(']', '').replace(' ', '')
        port = str(parameter_list[i * 4 + 2]).replace('[', '').replace(']', '').replace(' ', '')
        acct_name = str(parameter_list[i * 4 + 3]).replace('[', '').replace(']', '').replace(' ', '')
        if group_num == 1:
            return f"select b.ip_addr,b.db_name,b.instance_name, b.db_id, a.acct_name, a.acct_pwd from uap_db_acct a left join uap_db_host b	on a.db_id = b.db_id right join (select * from uap_db_host	where ip_addr = '{ip_addr}' and port = '{port}' and  (instance_name = '{db_name}' or db_name = '{db_name}')) c on c.db_id = a.db_id and b.ip_addr = c.ip_addr and (b.db_name = c.db_name or b.instance_name = c.instance_name ) where acct_name = '{acct_name}'"
        if i == group_num - 1:
            result += f"select b.ip_addr,b.db_name,b.instance_name, b.db_id, a.acct_name, a.acct_pwd from uap_db_acct a left join uap_db_host b	on a.db_id = b.db_id right join (select * from uap_db_host	where ip_addr = '{ip_addr}' and port = '{port}' and (instance_name = '{db_name}' or db_name = '{db_name}')) c on c.db_id = a.db_id and b.ip_addr = c.ip_addr and (b.db_name = c.db_name or b.instance_name = c.instance_name ) where acct_name = '{acct_name}' and rownum < 999  "
            return result
        result += f"select b.ip_addr,b.db_name,b.instance_name, b.db_id, a.acct_name, a.acct_pwd from uap_db_acct a left join uap_db_host b	on a.db_id = b.db_id right join (select * from uap_db_host	where ip_addr = '{ip_addr}' and port = '{port}' and (instance_name = '{db_name}' or db_name = '{db_name}')) c on c.db_id = a.db_id and b.ip_addr = c.ip_addr and (b.db_name = c.db_name or b.instance_name = c.instance_name ) where acct_name = '{acct_name}' and rownum < 999 union all\n"

    return result


def set_password(args):
    parameter_list = list(args.strip().split())
    if not len(parameter_list):
        return ""
    if len(parameter_list) % 5:
        return "只能输入 ip或域名 端口 库名 从账号 加密密码 5个一组的数据！"

    group_num = int(len(parameter_list) / 5)
    print(group_num)

    result = ''
    if group_num > 1: result += 'begin\n'
    for i in range(group_num):
        db_name = str(parameter_list[i * 5]).replace('[', '').replace(']', '').replace(' ', '')
        ip_addr = str(parameter_list[i * 5 + 1]).replace('[', '').replace(']', '').replace(' ', '')
        port = str(parameter_list[i * 5 + 2]).replace('[', '').replace(']', '').replace(' ', '')
        acct_name = str(parameter_list[i * 5 + 3]).replace('[', '').replace(']', '').replace(' ', '')
        password = str(parameter_list[i * 5 + 4]).replace('[', '').replace(']', '').replace(' ', '')
        result += f"update uap_db_acct set acct_pwd ='{password}' where acct_name = '{acct_name}' and db_id = (select db_id from uap_db_host where ip_addr = '{ip_addr}' and (instance_name = '{db_name}' or db_name = '{db_name}') and port = '{port}');\n"

    if group_num > 1: result += 'end;'
    return result


def set_password_simple(args):
    parameter_list = list(args.strip().split())
    if not len(parameter_list):
        return ""
    if len(parameter_list) % 3:
        return "只能输入 从账号 db_id 加密密码 3个一组的数据！"

    group_num = int(len(parameter_list) / 3)
    print(group_num)

    result = ''
    if group_num > 1: result += 'begin\n'
    for i in range(group_num):
        acct_name = str(parameter_list[i * 3]).replace('[', '').replace(']', '').replace(' ', '')
        db_id = str(parameter_list[i * 3 + 1]).replace('[', '').replace(']', '').replace(' ', '')
        password = str(parameter_list[i * 3 + 2]).replace('[', '').replace(']', '').replace(' ', '')
        result += f"update uap_db_acct set acct_pwd ='{password}' where db_id = '{db_id}'and acct_name = '{acct_name}';\n"

    if group_num > 1: result += 'end;'
    return result
